package com.huaxin.common.jpush;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.huaxin.common.tools.Log;


/**
 * @author lwjing
 * @date 2018/6/26
 * @Desc $desc$
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class NotificationBroadcastReceiver extends BroadcastReceiver {

    public static final String TYPE = "type"; //这个type是为了Notification更新信息的，这个不明白的朋友可以去搜搜，很多

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int type = intent.getIntExtra(TYPE, -1);

        if (type != -1) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(type);
        }

        if (action.equals("notification_clicked")) {
            //处理点击事件
            String message = intent.getStringExtra("MESSAGE");
            clickNext(context,message);
        }

        if (action.equals("notification_cancelled")) {
            //处理滑动清除和点击删除事件
        }
    }

    /**
     * 通知栏打开app或跳转界面
     * @param context
     * @param message
     */
    private void clickNext(Context context,String message) {
        //判断应用是不是正在使用。若是则打开工单界面或者待办任务界面。若不是启动应用
        Log.e("jpush","isappAlive===="+HxJPushReceiver.isAppAlive(context) + message);
        if (HxJPushReceiver.isAppAlive(context)) {
            if (message.contains("[审批]")){
                HxJPushReceiver.toTaskWaitDoAct(context);
            }else if (message.contains("[工单]")){
                HxJPushReceiver.toArrangeTaskAct(context);
            }else{
                HxJPushReceiver.toHomeAct(context);
            }

        } else {
            HxJPushReceiver.startApp(context);
        }
    }

}
